<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/6/3
 * Time: 15:49
 */

namespace Weixin\Service;


use Common\Common\Service;
use Weixin\Component\User\UserComponent;
use Weixin\Exception\WeixinException;
use Weixin\Request\User\AddUserRequest;
use Weixin\Response\AddUserResponse;

class AddUserService implements Service
{
    public function run(AddUserRequest $request = null)
    {
        if ($request->issetNickname() == false)
            throw new WeixinException(10001);

        $nickname = $request->getNickname();
        $last_time = date('Y-m-d H:i:s', time());
        $component = new UserComponent();
        $response = new AddUserResponse();
        try {
            $data = $component->addUser($nickname, $last_time);
            if (is_null($data) == true) {
                $response->setItIsNull();
            } else {
                $response->setData($data);
            }
        } catch (WeixinException $e) {
            throw new WeixinException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}